//
//  ViewController.m
//  19-JSON
//
//  Created by dllo on 16/8/9.
//  Copyright © 2016年 ZHW. All rights reserved.
//

#import "ViewController.h"
#import "CustomCollectionViewCell.h"
#import "CustomCollectionViewLayout.h"
#import "Model.h"
#import "NewsModel.h"
#import "SelfSizeTool.h"
@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, CustomLayoutDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *myCollectionView;
@property (nonatomic, strong) NSMutableArray<Model *> *dataArray;
@property (nonatomic, strong) NSMutableArray<NewsModel *> *newsArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataArray = [NSMutableArray array];
    self.newsArray = [NSMutableArray array];
    [self getDataFromWYNews];
    
    CustomCollectionViewLayout *layout = (CustomCollectionViewLayout *)_myCollectionView.collectionViewLayout;
    layout.delegate = self;
}

- (void)getDataFromWYNews {
    NSString *newsJSON = [[NSBundle mainBundle] pathForResource:@"WYNews" ofType:@"json"];
    NSData *newsData = [NSData dataWithContentsOfFile:newsJSON];
    NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:newsData options:NSJSONReadingMutableContainers error:nil];
    NSArray *tListArr = [jsonDic objectForKey:@"tList"];
    for (NSDictionary *dictionary in tListArr) {
        NewsModel *newsModel = [NewsModel newsModelWithDictionary:dictionary];
        [_newsArray addObject:newsModel];
    }
}

- (void)getDataFromJSON {
    // JSON
    // 通过一个Data 将JSON格式转换成OC内容
    NSString *dataPath = [[NSBundle mainBundle] pathForResource:@"FirstJSON" ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:dataPath];
    NSError *error = nil;
    NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
    NSDictionary *tabsDic = [jsonDic objectForKey:@"tabs"];
    NSArray *listArray = [tabsDic objectForKey:@"list"];
    for (NSDictionary * dictionary in listArray) {
        Model *model = [Model modelWithDictionary:dictionary];
        [_dataArray addObject:model];
    }
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout
                   height:(CGFloat)height
  widthForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSString *text = [_newsArray[indexPath.row] tname];
    CGFloat width = [SelfSizeTool selfSizeWidthWithString:text height:38 font:[UIFont systemFontOfSize:17]];
    CGFloat widthItem = ceilf(width);
    return widthItem;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _newsArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CustomCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    NewsModel *newsModel = _newsArray[indexPath.row];
    cell.newsModel = newsModel;
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
